﻿using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class Base<T>:IBase<T> where T : class
    {
        public Base(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t">参数</param>
        /// <returns>返回</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddAsync(T t)
        {
            Context.Set<T>().Add(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="t">参数</param>
        /// <returns>返回</returns>
        public async Task<int> DeleteAllAsync(List<T> t)
        {
            Context.Set<T>().RemoveRange(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>返回</returns>
        public async Task<int> DeleteAsync(int id)
        {
            var obj= Context.Set<T>().Find(id);
            Context.Set<T>().RemoveRange(obj);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 查全部
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
           return Context.Set<T>();
        }
        /// <summary>
        /// 根据一个编号查出一条数据
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>返回</returns>
        public T GetValue(int id)
        {
            return Context.Set<T>().Find(id);
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="t">参数</param>
        /// <returns>返回</returns>
        public async Task<int> UpdateAllAsync(List<T> t)
        {
            Context.Set<T>().UpdateRange(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t">参数</param>
        /// <returns>返回</returns>
        public async Task<int> UpdateAsync(T t)
        {
            Context.Set<T>().Update(t);
            return await Context.SaveChangesAsync();
        }
    }
}
